//
//  YZECommunityTableViewCell2.m
//  yilutongxing
//
//  Created by ios-23 on 17/4/5.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZECommunityTableViewCell2.h"
#import "UIImage+QMUI.h"
#import "PhotosView.h"
#import "BaseModel.h"
#import "CommonDefine.h"
#import "UIView+Layout.h"

#define BASE_HEIGHT 142

@interface YZECommunityTableViewCell2 ()

@property (nonatomic, retain) NSMutableArray *imageArr;
@property (nonatomic, assign) CGFloat picHeight;
@end

@implementation YZECommunityTableViewCell2

- (CGFloat)cellHeight:(BaseModel*)model superWidth:(CGFloat)width{
    NSMutableArray *imgUrlArr = [NSMutableArray arrayWithArray:[model.imageurls componentsSeparatedByString:@","]];
    if (imgUrlArr.count>0 && [NSString stringWithString:[imgUrlArr lastObject]].length==0) {
        [imgUrlArr removeLastObject];
    }
    self.picHeight = 0;
    CGFloat picwidth = (width - 50 - 12 - 16)/3;
    if (imgUrlArr.count>0 && imgUrlArr.count<4) {
        self.picHeight = picwidth;
    }
    else if (imgUrlArr.count>3 && imgUrlArr.count<7) {
        self.picHeight = picwidth*2 + 8;
    }
    
    return _contentLable.y + _contentLable.height + 5 + self.picHeight + 5 + _createdDateLable.height + 6;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)showData:(BaseModel*)model superWidth:(CGFloat)width{
    //数据
    NSString *str = model.name;
    self.nameLable.text = str;
    self.createdByLable.text = model.createdUser.firstName;
    self.createdDateLable.text = model.createdDate;
    self.contentLable.text = model.content;
    self.replyCountLabel.text = model.replyCount;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.createdUser.avatar]];
    //布局
    CGFloat cellH = [self cellHeight:model superWidth:width];
    CGFloat cellW = width;
    self.cellbox.size = CGSizeMake(cellW, cellH);
    self.headImageView.frame = CGRectMake(6, 8, 40, 40);
    self.headImageView.layer.masksToBounds=YES;
    self.headImageView.layer.cornerRadius=self.headImageView.width/2;
    
    self.createdByLable.frame = CGRectMake(53, 20, 154, 16);
    self.nameLable.frame = CGRectMake(53, 42, self.cellbox.width - 53 - 8, 20);
    self.contentLable.frame = CGRectMake(53, 70, self.cellbox.width - 53 - 8, 37);
    
    if (model.imageurls) {
        NSString *urlArrStr = model.imageurls;
        NSMutableArray *urlArr = [NSMutableArray arrayWithArray:[urlArrStr componentsSeparatedByString:@","]];
        if (urlArr.count>0 && [NSString stringWithString:[urlArr lastObject]].length==0) {
            [urlArr removeLastObject];
        }
        //构造imageview
        if (!_picsView) {
            _picsView = [[PhotosView alloc]initWithFrame:CGRectMake(53, _contentLable.y + _contentLable.height + 5, width - 53 - 8, self.picHeight)];
        }
        self.picsView.hidden = NO;
        self.picsView.pic_urls = urlArr;
        self.picsView.tag = 666;
        [self.cellbox addSubview:self.picsView];
        
        self.createdDateLable.frame = CGRectMake(53, self.picsView.frame.origin.y + self.picHeight + 5, 157, 15);
        self.replyCountLabel.frame = CGRectMake(cellW - 8 - 41, self.createdDateLable.frame.origin.y, 41, 12);
        self.comment_icon.frame = CGRectMake(self.replyCountLabel.frame.origin.x - 20, self.createdDateLable.frame.origin.y, 16, 16);
    }
    else {
        self.picsView.hidden = YES;
        self.createdDateLable.frame = CGRectMake(53, 117, 157, 15);
        self.replyCountLabel.frame = CGRectMake(self.cellbox.frame.size.width - 8 - 41, 117, 41, 12);
        self.comment_icon.frame = CGRectMake(self.replyCountLabel.frame.origin.x - 20, 117, 16, 16);
    }
    self.bottomline.frame = CGRectMake(0, cellH - 1, cellW, 1);
}


@end



























